package com.nms.action;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.nms.model.User;
import com.nms.model.UserInfo;
import com.nms.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserService userService;
	private User user;
	private UserInfo userInfo;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public UserService getUserService() {
		return userService;
	}
	
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/////////////////////////////////////////////////////
	public String showIndexPage() {
		return "showIndexPage";
	}

	public String login() {
		if ((user = userService.login(user)) != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
			session.put("user", user);
			session.put("loginTime", new Date());
			return "loginSuccess";
		}
		return "loginFail";
	}

	public String logout() {
		Map<String, Object> session = ActionContext.getContext()
				.getSession();
		if(session.get("user") != null){
			session.put("user", null);
			session.put("loginTime", null);
		}
		return "logout";
	}

	public String register() {
		user.setUserInfo(userInfo);
		switch(userService.register(user)) {
		case 0 : ;break;
		case 1 : return "registerSuccess";
		default :break;
		}
		return "registerFail";
	}

	public String showNews() {
		return "showNews";
	}

}
